package com.xbongbong.pro.aiassistant.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.constant.TimeConstant;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.domain.entity.ext.ConditionsEntityExt;
import com.xbongbong.paas.domain.entity.ext.OptionalRangeEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.elasticsearch.util.EsHelper;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.enums.OptionalRangeEnum;
import com.xbongbong.paas.help.UserAndDepartmentHelp;
import com.xbongbong.paas.help.subform.TransferSubFormHelper;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.paas.toolbox.util.JsonHelperUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.aiassistant.handle.SaasAiAssistantStrategyHandle;
import com.xbongbong.pro.aiassistant.help.SsServiceHelp;
import com.xbongbong.pro.aiassistant.manager.feign.BusinessFeignClient;
import com.xbongbong.pro.aiassistant.service.SsExecutorService;
import com.xbongbong.pro.constant.DynamicManagerConstant;
import com.xbongbong.pro.domain.entity.AiAssistantEntity;
import com.xbongbong.pro.enums.DynamicManagerLevelEnum;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.smartsettings.pojo.SsExecutorPojo;
import com.xbongbong.pro.smartsettings.pojo.SsHistoryValidPojo;
import com.xbongbong.pro.smartsettings.pojo.dto.SmartStrategyRabbitMqDTO;
import com.xbongbong.pro.weblist.pojo.WorkReportUserStatisicPojo;
import com.xbongbong.pro.weblist.pojo.dto.WorkReportUserStatisicDTO;
import com.xbongbong.pro.weblist.pojo.vo.WorkReportUserStatisicVO;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.entity.ProductWarehouseEntity;
import com.xbongbong.saas.domain.entity.UserTeamEntity;
import com.xbongbong.saas.enums.UserTeamEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.ContractEnum;
import com.xbongbong.saas.enums.business.PayPlanEnum;
import com.xbongbong.saas.enums.business.PaymentEnum;
import com.xbongbong.saas.enums.business.ProductEnum;
import com.xbongbong.saas.enums.business.ProductionOrderEnum;
import com.xbongbong.saas.enums.business.PurchaseEnum;
import com.xbongbong.saas.enums.business.WorkReportTypeEnum;
import com.xbongbong.saas.enums.subform.ProductWarehouseSubFormEnum;
import com.xbongbong.saas.service.toolbox.help.UserTeamHelp;
import com.xbongbong.sys.domain.entity.DepartmentEntity;
import com.xbongbong.sys.domain.entity.UserDepartmentEntity;
import com.xbongbong.sys.domain.entity.UserEntity;
import com.xbongbong.sys.model.UserModel;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.elasticsearch.index.query.QueryBuilders.termsQuery;


/**
 * @description: 获取关联执行人
 * @Author: hongxiao
 * @date: 2019-05-11 11:32
 * @since v1.0
 */
@Service("ssExecutorService")
public class SsExecutorServiceImpl implements SsExecutorService {
    private static final Logger LOG = LoggerFactory.getLogger(SsExecutorServiceImpl.class);

    /**
     * 最大主管级别
     */
    private static final int MAX_LEVEL_LEADER = 4;
    /**
     * 缓存常量
     */


    @Resource
    private UserModel userModel;
    @Resource
    private UserTeamHelp userTeamHelp;
    @Resource
    private EsHelper esHelper;
    @Resource
    private UserAndDepartmentHelp userAndDepartmentHelp;
    @Resource
    private SsServiceHelp ssServiceHelp;
    @Resource
    private BusinessFeignClient businessFeignClient;
    @Resource
    private TransferSubFormHelper transferSubFormHelper;
    @Resource
    private SaasAiAssistantStrategyHandle saasAiAssistantStrategyHandle;

    @Override
    public List<SsExecutorPojo> getExecutorList(SmartStrategyRabbitMqDTO rabbitMqDTO, List<SsHistoryValidPojo> list,
                                                Map<Integer, List<PaasFormDataEntityExt>> saasDataList,
                                                List<PaasFormDataEntityExt> paasDataList, Map<Long, SsExecutorPojo> workReportExecutorListMap) {
        String corpid = rabbitMqDTO.getCorpid();
        List<AiAssistantEntity> smartList = new ArrayList<>(rabbitMqDTO.getList());

        // 部门id/角色id列表（查全公司使用）
        Set<Long> deptIdList = new HashSet<>();
        Set<Integer> roleIdList = new HashSet<>();
        // 智能助手对应的businessType
        Map<Long, Integer> businessTypeMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        // 智能助手对应的员工ids/部门ids/角色ids
        Map<Long, Set<String>> userIdsMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        Map<Long, Set<Long>> deptIdsMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        Map<Long, Set<Integer>> roleIdsMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        // 智能助手对应的关联负责人levels(负责人、协同人、创建人、关联合同、关联仓库、关联采购合同)
        Map<Long, Set<Integer>> managerLevelMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        Map<Long, Set<Integer>> coordinatorLevelMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        Map<Long, Set<Integer>> creatorLevelMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        Map<Long, Set<Integer>> linkContractLevelMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        Map<Long, Set<Integer>> linkWareHouseLevelMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        Map<Long, Set<Integer>> linkPurchaseLevelMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        // 设置对应的数据（员工/部门/角色/关联负责人）
        getExecutorTypeData(smartList, deptIdList, roleIdList, businessTypeMap, userIdsMap, deptIdsMap, roleIdsMap,
                managerLevelMap, coordinatorLevelMap, creatorLevelMap, linkContractLevelMap, linkWareHouseLevelMap, linkPurchaseLevelMap);


        // 部门对应员工
        Map<Long, List<String>> userIdsForDept = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        if (deptIdList.size() > 0) {
            List<Map<String, Object>> userIdsForDepMapList = userModel.getUserIdsJoinDeptGroup(corpid, deptIdList);
            for (Map<String, Object> userIdsForDepMap : userIdsForDepMapList) {
                Long depId = (Long) userIdsForDepMap.get("depId");
                String userIdsStr = (String) userIdsForDepMap.get("userIds");
                List<String> userIds = StringUtil.toStringList(userIdsStr, StringConstant.COMMA);
                userIdsForDept.put(depId, userIds);
            }
        }

        // 角色对应的员工
        Map<Integer, List<String>> userIdsForRole = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        if (roleIdList.size() > 0) {
            Map<Integer, List<String>> roleUserMap = userAndDepartmentHelp.userIdsForRole(corpid);
            for (Integer roleId : roleIdList) {
                if (roleUserMap.containsKey(roleId)) {
                    userIdsForRole.put(roleId, roleUserMap.get(roleId));
                }
            }
        }


        // 短信，工作报告通知人员另外处理
        // 然后再分模块处理对应的动态负责人(SAAS,PAAS,短信,工作报告)
        // 过虑执行动作为短信的智能助手
        List<AiAssistantEntity> smsSmartList = new ArrayList<>();
        List<AiAssistantEntity> workReportSmartList = new ArrayList<>();
        List<AiAssistantEntity> saasSmartList = new ArrayList<>();
        List<AiAssistantEntity> paasSmartList = new ArrayList<>();

        ssServiceHelp.getSmartSettingsForCategory(smartList, smsSmartList, workReportSmartList, saasSmartList, paasSmartList);

        // 先取dataId
        Map<Long, Set<Long>> dataIdsMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        list.forEach(item -> {
            Set<Long> dataIds = new HashSet<>();
            item.getAddList().forEach(ssHistoryEntity -> {
                dataIds.add(ssHistoryEntity.getDataId());
            });
            item.getUpdateList().forEach(ssHistoryEntity -> {
                dataIds.add(ssHistoryEntity.getDataId());
            });
            dataIdsMap.put(item.getSmartSettingsId(), dataIds);
        });


        // 处理SAAS执行人
        List<SsExecutorPojo> saasExecutorList = getSaasExecutorList(corpid, saasSmartList, saasDataList,
                businessTypeMap, managerLevelMap, coordinatorLevelMap, creatorLevelMap, linkContractLevelMap, linkWareHouseLevelMap, linkPurchaseLevelMap, dataIdsMap,
                userIdsForDept, userIdsForRole, userIdsMap, deptIdsMap, roleIdsMap);
        // 处理PAAS执行人
        List<SsExecutorPojo> paasExecutorList = getPaasExecutorList(corpid, paasSmartList, paasDataList, managerLevelMap, coordinatorLevelMap, creatorLevelMap, dataIdsMap,
                userIdsForDept, userIdsForRole, userIdsMap, deptIdsMap, roleIdsMap);
        // 处理工作报告执行人
        getWorkReportExecutorList(corpid, workReportSmartList, userIdsForDept, userIdsForRole, userIdsMap, deptIdsMap, roleIdsMap, workReportExecutorListMap);

        List<SsExecutorPojo> executorList = new ArrayList<>();
        executorList.addAll(saasExecutorList);
        executorList.addAll(paasExecutorList);

        return executorList;

        // TODO 动态负责人（关联业务）
        /*
         * 对应业务数据的关联负责人等
         * 1.saas入库的负责人按businessType聚合数据id
         * 2.paas及saas未入库的负责人直接聚合数据id
         * 3.特殊逻辑处理
         * 3.1回款到期/回款逾期--关联合同负责人；
         * 3.2采购到货--关联仓库负责人；
         * 3.3待采购未处理--关联合同负责人
         * 3.4采购合同--关联仓库负责人
         * 3.5生产完成--关联仓库负责人
         * 3.6待生产未处理--关联合同负责人
         * 3.7待出库未处理--关联合同负责人，关联仓库负责人
         * 3.8待入库未处理--关联采购合同负责人，关联仓库负责人
         * 3.9产品库存预警/产品过期预警--关联仓库负责人
         */

    }

    /**
     * 获取相关的执行人数据
     *
     * @param smartList             智能助手列表
     * @param deptIdList            所有部门列表
     * @param roleIdList            所有角色列表
     * @param businessTypeMap       saas业务类型map{smartId:businessTyp}
     * @param userIdsMap            员工map{smartId:[userId1,userId2]}
     * @param deptIdsMap            部门map{smartId:[deptId1,deptId2]}
     * @param roleIdsMap            角色map{smartId:[roleId1,roleId2]}
     * @param managerLevelMap       负责人map{smartId:[level1,level2]}
     * @param coordinatorLevelMap   协同人map{smartId:[level1,level2]}
     * @param creatorLevelMap       创建人map{smartId:[level1,level2]}
     * @param linkContractLevelMap  关联合同负责人map{smartId:[level1,level2]}
     * @param linkWareHouseLevelMap 关联仓库负责人map{smartId:[level1,level2]}
     * @param linkPurchaseLevelMap  关联采购合同负责人map{smartId:[level1,level2]}
     * @return
     * @throws
     * @author hongxiao
     * @date 2019-05-21 19:49
     * @version v1.0
     * @since v1.0
     */
    private void getExecutorTypeData(List<AiAssistantEntity> smartList, Set<Long> deptIdList, Set<Integer> roleIdList,
                                     Map<Long, Integer> businessTypeMap, Map<Long, Set<String>> userIdsMap, Map<Long, Set<Long>> deptIdsMap,
                                     Map<Long, Set<Integer>> roleIdsMap, Map<Long, Set<Integer>> managerLevelMap, Map<Long, Set<Integer>> coordinatorLevelMap,
                                     Map<Long, Set<Integer>> creatorLevelMap, Map<Long, Set<Integer>> linkContractLevelMap,
                                     Map<Long, Set<Integer>> linkWareHouseLevelMap, Map<Long, Set<Integer>> linkPurchaseLevelMap) {
        List<String> subAliasList = Arrays.asList(FieldTypeEnum.OWNERID.getAlias(), FieldTypeEnum.COUSERID.getAlias(), FieldTypeEnum.CREATORID.getAlias(),
                DynamicManagerConstant.LINK_CONTRACT_ALIAS, DynamicManagerConstant.LINK_WARE_HOUSE_ALIAS, DynamicManagerConstant.LINK_WARE_HOUSE_ALIAS);
        smartList.forEach(item -> {
            List<OptionalRangeEntity> rangeList = item.getExecutor().toJavaList(OptionalRangeEntity.class);
            Set<Long> deptIds = new HashSet<>();
            Set<Integer> roleIds = new HashSet<>();
            Set<String> userIds = new HashSet<>();
            Set<Integer> managerLevels = new HashSet<>();
            Set<Integer> coordinatorLevels = new HashSet<>();
            Set<Integer> creatorLevels = new HashSet<>();
            Set<Integer> linkContractLevels = new HashSet<>();
            Set<Integer> linkWareHouseLevels = new HashSet<>();
            Set<Integer> linkPurchaseLevels = new HashSet<>();
            rangeList.forEach(rangeEntity -> {
                OptionalRangeEnum rangeEnum = OptionalRangeEnum.getByValue(rangeEntity.getProperty());
                String attrAlias = rangeEntity.getAttr();

                if (subAliasList.contains(attrAlias)) {
                    // 动态负责人
                    Integer code = Integer.parseInt(rangeEntity.getId());
                    if (Objects.equals(FieldTypeEnum.OWNERID.getAlias(), attrAlias)) {
                        managerLevels.add(code);
                    } else if (Objects.equals(FieldTypeEnum.COUSERID.getAlias(), attrAlias)) {
                        coordinatorLevels.add(code);
                    } else if (Objects.equals(FieldTypeEnum.CREATORID.getAlias(), attrAlias)) {
                        creatorLevels.add(code);
                    } else if (Objects.equals(DynamicManagerConstant.LINK_CONTRACT_ALIAS, attrAlias)) {
                        linkContractLevels.add(code);
                    } else if (Objects.equals(DynamicManagerConstant.LINK_WARE_HOUSE_ALIAS, attrAlias)) {
                        linkWareHouseLevels.add(code);
                    } else if (Objects.equals(DynamicManagerConstant.LINK_PURCHASE_ALIAS, attrAlias)) {
                        linkPurchaseLevels.add(code);
                    }
                } else if (rangeEnum != null) {
                    switch (rangeEnum) {
                        case DEPT:
                            Long deptId = Long.parseLong(rangeEntity.getId());
                            deptIdList.add(deptId);
                            deptIds.add(deptId);
                            break;
                        case USER:
                            userIds.add(rangeEntity.getId());
                            break;
                        case ROLE:
                            Integer roleId = Integer.parseInt(rangeEntity.getId());
                            roleIdList.add(roleId);
                            roleIds.add(roleId);
                            break;
                        default:
                            break;
                    }
                }
            });

            // 智能助手对应的部门ids
            deptIdsMap.put(item.getId(), deptIds);
            // 智能助手对应的员工ids
            userIdsMap.put(item.getId(), userIds);
            // 智能助手对应的角色ids
            roleIdsMap.put(item.getId(), roleIds);
            // 智能助手对应的关联负责人levels
            if (managerLevels.size() > 0) {
                managerLevelMap.put(item.getId(), managerLevels);
            }
            if (coordinatorLevels.size() > 0) {
                coordinatorLevelMap.put(item.getId(), coordinatorLevels);
            }
            if (creatorLevels.size() > 0) {
                creatorLevelMap.put(item.getId(), creatorLevels);
            }
            if (linkContractLevels.size() > 0) {
                linkContractLevelMap.put(item.getId(), linkContractLevels);
            }
            if (linkWareHouseLevels.size() > 0) {
                linkWareHouseLevelMap.put(item.getId(), linkWareHouseLevels);
            }
            if (linkPurchaseLevels.size() > 0) {
                linkPurchaseLevelMap.put(item.getId(), linkPurchaseLevels);
            }

            // 智能助手对应的业务类型（businessType）
            if (item.getSaasMark().equals(SaasMarkEnum.SAAS.getCode())) {
                businessTypeMap.put(item.getId(), item.getBusinessType());
            }
        });
    }

    /**
     * 获取员工实体列表
     *
     * @param allList   所有员工
     * @param userIdSet 员工ids
     * @return
     * @throws
     * @author hongxiao
     * @date 2019-06-01 16:23
     * @version v1.0
     * @since v1.0
     */
    private List<UserEntity> getByUserIds(List<UserEntity> allList, Set<String> userIdSet) {
        List<UserEntity> list = new ArrayList<>();
        if (Objects.nonNull(allList) && !allList.isEmpty()) {
            allList.forEach(item -> {
                if (userIdSet.contains(item.getUserId())) {
                    list.add(item);
                }
            });
        }
        return list;
    }

    /**
     * 获取关联负责人对应的数据ids
     *
     * @param levelMap        关联负责人：{smartId: managerLevels}
     * @param dataIdsMap      数据ids:{smartId: dataIds}
     * @param businessTypeMap 业务类型：{samrtId: businessType}
     * @param levelDataIdsMap 关联负责人数据ids:{businessType: dataIds}
     * @param levelDataIds    数据ids
     * @return
     * @throws
     * @author hongxiao
     * @date 2019-06-01 16:17
     * @version v1.0
     * @since v1.0
     */
    private void getLevelDataIds(Map<Long, Set<Integer>> levelMap, Map<Long, Set<Long>> dataIdsMap, Map<Long, Integer> businessTypeMap,
                                 Map<Integer, Set<Long>> levelDataIdsMap, Set<Long> levelDataIds) {
        levelMap.forEach((smartId, managerLevels) -> {
            Set<Long> dataIds = dataIdsMap.get(smartId);
            if (Objects.nonNull(dataIds)) {
                Set<Long> newDataIds = new HashSet<>(dataIds);
                if (businessTypeMap.containsKey(smartId)) {
                    Integer businessType = businessTypeMap.get(smartId);

                    UserTeamEnum userTeamEnum = UserTeamEnum.getByCode(businessType);
                    if (userTeamEnum == UserTeamEnum.UNKNOW) {
                        levelDataIds.addAll(newDataIds);
                    } else {
                        if (levelDataIdsMap.containsKey(businessType)) {
                            levelDataIdsMap.get(businessType).addAll(newDataIds);
                        } else {
                            levelDataIdsMap.put(businessType, newDataIds);
                        }
                    }
                } else {
                    levelDataIds.addAll(newDataIds);
                }
            }
        });
    }

    /**
     * 获取关联合同、仓库的负责人对应的数据ids
     *
     * @param levelMap        关联负责人map:{smartId:levels}
     * @param dataIdsMap      数据ids: {smartId:dataIds}
     * @param businessTypeMap 业务类型{smartId:businessType}
     * @param levelDataIdsMap 返回的数据：{businessTyep:dataIds}
     * @return
     * @throws
     * @author hongxiao
     * @date 2019-06-01 16:12
     * @version v1.0
     * @since v1.0
     */
    private void getLinkLevelDatdIds(Map<Long, Set<Integer>> levelMap, Map<Long, Set<Long>> dataIdsMap, Map<Long, Integer> businessTypeMap,
                                     Map<Integer, Set<Long>> levelDataIdsMap) {
        levelMap.forEach((smartId, levels) -> {
            Set<Long> dataIds = dataIdsMap.get(smartId);
            if (Objects.nonNull(dataIds) && businessTypeMap.containsKey(smartId)) {
                Integer businessType = businessTypeMap.get(smartId);
                if (levelDataIdsMap.containsKey(businessType)) {
                    levelDataIdsMap.get(businessType).addAll(dataIds);
                } else {
                    levelDataIdsMap.put(businessType, dataIds);
                }
            }
        });
    }

    /**
     * 获取关联合同的负责人ids
     *
     * @param corpid          公司id
     * @param formDataListMap 数据列表
     * @param linkDataIdsMap  关联合同的应收款ids/关联采购合同的付款计划ids：{businessTyep:dataIds}
     * @return {paymentId/payPlanId: userIds} 应收款对应合同负责人（付款计划对应的采购合同负责人）的userIds
     * @throws
     * @author hongxiao
     * @date 2019-06-01 16:09
     * @version v1.0
     * @since v1.0
     */
    private Map<Long, Set<String>> getLinkContrOrPurUserIds(String corpid, Map<Integer, List<PaasFormDataEntityExt>> formDataListMap,
                                                            Map<Integer, Set<Long>> linkDataIdsMap, XbbRefTypeEnum refTypeEnum) {

        Map<Long, Set<String>> linkUserMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        if (linkDataIdsMap.size() > 0) {
            Set<Long> dataIdSet = new HashSet<>();
            linkDataIdsMap.forEach((businessType, dataIds) -> {
                dataIdSet.addAll(dataIds);
            });

            // 回款id对应合同id的map
            Map<Long, Long> payment2ContractMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            Set<Long> contractIdSet = new HashSet<>();

            // 关联的合同/采购合同类型
            Integer linkBusinessType = XbbRefTypeEnum.CONTRACT.getCode();

            if (formDataListMap.containsKey(refTypeEnum.getCode())) {
                List<PaasFormDataEntityExt> formDataList = formDataListMap.get(refTypeEnum.getCode());
                String attr = PaymentEnum.CONTRACT.getAttr();
                if (XbbRefTypeEnum.PAY_PLAN == refTypeEnum) {
                    attr = PayPlanEnum.LINK_PURCHASE.getAttr();
                    linkBusinessType = XbbRefTypeEnum.PURCHASE.getCode();
                }
                // 获取对应的合同/采购合同
                for (PaasFormDataEntityExt entityExt : formDataList) {
                    Long dataId = entityExt.getId();
                    JSONObject dataList = entityExt.getData();
                    if (dataIdSet.contains(dataId)) {
                        Long contractId = dataList.getLong(attr);
                        contractIdSet.add(contractId);
                        payment2ContractMap.put(dataId, contractId);
                    }
                }
            }

            Map<Long, Set<String>> userMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            List<UserTeamEntity> userTeamList = userTeamHelp.getUserTeamList(contractIdSet, corpid, linkBusinessType, false, 1);
            userTeamList.forEach(item -> {
                Long dataId = item.getDataId();
                if (userMap.containsKey(dataId)) {
                    userMap.get(dataId).add(item.getUserId());
                } else {
                    Set<String> userList = new HashSet<>();
                    userList.add(item.getUserId());
                    userMap.put(dataId, userList);
                }
            });

            payment2ContractMap.forEach((paymentId, contractId) -> {
                Set<String> contractUserIds = userMap.get(contractId);
                if (CollectionsUtil.isNotEmpty(contractUserIds)) {
                    linkUserMap.put(paymentId, contractUserIds);
                }
            });
        }

        return linkUserMap;
    }

    /**
     * @param userTeamMap 员工团队map:{dataId: [userId1,userId2]}
     * @param userId      员工id
     * @param dataId      数据id
     * @return
     * @throws
     * @author hongxiao
     * @date 2019-08-09 09:28
     * @version v1.0
     * @since v1.0
     */
    private void setUserTeam(Map<Long, List<String>> userTeamMap, String userId, Long dataId) {
        if (userTeamMap.containsKey(dataId)) {
            List<String> userTeamIds = userTeamMap.get(dataId);
            userTeamIds.add(userId);
            userTeamMap.put(dataId, userTeamIds);
        } else {
            List<String> userTeamIds = new ArrayList<>();
            userTeamIds.add(userId);
            userTeamMap.put(dataId, userTeamIds);
        }
    }

    /**
     * 获取关联的产品id
     *
     * @param formDataList 业务数据列表
     * @param dataIdSet    业务数据idSet
     * @param businessType 业务类型
     * @param attr         关联产品的attr
     * @param productSet   产品idSet
     * @param productMap   业务对应的产品id {businessType: {dataId: [productId1,productId1]}}
     * @return
     * @throws
     * @author hongxiao
     * @date 2019-08-16 22:15
     * @version v1.0
     * @since v1.0
     */
    private void getLinkProduct(List<PaasFormDataEntityExt> formDataList, Set<Long> dataIdSet, Integer businessType, String attr,
                                Set<Long> productSet, Map<Integer, Map<Long, Set<Long>>> productMap) {
        if (Objects.isNull(formDataList) || formDataList.isEmpty()) {
            return;
        }

        for (PaasFormDataEntityExt entityExt : formDataList) {
            Long dataId = entityExt.getId();
            JSONObject dataList = entityExt.getData();
            List<Long> productIds = new ArrayList<>();

            if (dataIdSet.contains(dataId)) {
                if (attr.isEmpty()) {
                    productIds.add(dataId);
                } else {
                    productIds = dataList.getJSONArray(attr).toJavaList(Long.class);
                }
            }
            // 所有的产品id
            productSet.addAll(productIds);

            Map<Long, Set<Long>> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            map.put(dataId, new HashSet<>(productIds));

            if (productMap.containsKey(businessType)) {
                Map<Long, Set<Long>> subMap = productMap.get(businessType);
                if (subMap.containsKey(dataId)) {
                    Set<Long> productIdSet = subMap.get(dataId);
                    productIdSet.addAll(productIds);
                    subMap.put(dataId, productIdSet);
                } else {
                    subMap.put(dataId, new HashSet<>(productIds));
                }
                productMap.put(businessType, subMap);
            } else {
                productMap.put(businessType, map);
            }
        }
    }

    /**
     * 获取关联业务数据对应的仓库负责人
     *
     * @param productUserIdsMap       产品对应的仓库负责人ids{productId:[userId1,userId2]}
     * @param dataIdProductIdsMap     业务数据对应的产品ids{dataId:[productId1,productId2]}
     * @param businessType            业务类型
     * @param linkWarehouseUserIdsMap 返回数据，业务数据对应的仓库负责人ids{businessType:{dataId1:[userId1,userId2]}}
     * @return
     * @throws
     * @author hongxiao
     * @date 2019-08-19 11:29
     * @version v1.0
     * @since v1.0
     */
    private void linkWarehouseUserIds(Map<Long, Set<String>> productUserIdsMap, Map<Long, Set<Long>> dataIdProductIdsMap,
                                         Integer businessType, Map<Integer, Map<Long, Set<String>>> linkWarehouseUserIdsMap) {

        for (Map.Entry<Long, Set<Long>> entry : dataIdProductIdsMap.entrySet()) {
            Long dataId = entry.getKey();
            Set<Long> productIds = entry.getValue();
            // 负责人Set
            Set<String> userIds = new HashSet<>();

            for (Map.Entry<Long, Set<String>> userIdsMapEntry : productUserIdsMap.entrySet()) {
                if (productIds.contains(userIdsMapEntry.getKey())) {
                    // 根据产品id拿到对应的仓库负责人
                    userIds.addAll(userIdsMapEntry.getValue());
                }
            }
            // 业务数据对应关联产品的负责人
            Map<Long, Set<String>> dataUserIdsMap = new HashMap<>();
            dataUserIdsMap.put(dataId, userIds);
            if (linkWarehouseUserIdsMap.containsKey(businessType)) {
                Map<Long, Set<String>> subMap = linkWarehouseUserIdsMap.get(businessType);
                if (subMap.containsKey(dataId)) {
                    Set<String> subUserIds = subMap.get(dataId);
                    userIds.addAll(subUserIds);
                    // 单条数据的负责人列表
                    subMap.put(dataId, userIds);
                } else {
                    // 单条数据的负责人列表
                    subMap.put(dataId, new HashSet<>(userIds));
                }
                // 每个业务对应数据的负责列表
                linkWarehouseUserIdsMap.put(businessType, subMap);
            } else {
                // 每个业务对应数据的负责列表
                linkWarehouseUserIdsMap.put(businessType, dataUserIdsMap);
            }
        }
    }


    /**
     * 获取关联仓库负责人
     *
     * @param corpid 公司id
     * @param linkWareHouseDataIdsMap 关联仓库的数据ids{businessType:[dataId1,dataId2]}
     * @param saasDataList 业务数据列表{businessType:[formDataEntityExt1,formDataEntityExt2]}
     * @return
     * @throws
     * @author hongxiao
     * @date 2019-08-19 14:26
     * @since v1.0
     * @version v1.0
     */
    private Map<Integer, Map<Long, Set<String>>> getLinkWarehouseUserId(String corpid, Map<Integer, Set<Long>> linkWareHouseDataIdsMap, Map<Integer, List<PaasFormDataEntityExt>> saasDataList) {

        // 仓库负责人
        Map<Integer, Map<Long, Set<String>>> linkWarehouseUserIdsMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        if (linkWareHouseDataIdsMap.size() > 0) {
            Set<Long> productIdSet = new HashSet<>();
            Map<Integer, Map<Long, Set<Long>>> productMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);

            Set<Long> contractIds = new HashSet<>();
            Set<Long> purchaseIds = new HashSet<>();
            Set<Long> productionOrderIds = new HashSet<>();
            Set<Long> productIds = new HashSet<>();

            for (Map.Entry<Integer, Set<Long>> entry : linkWareHouseDataIdsMap.entrySet()) {
                XbbRefTypeEnum refTypeEnum = XbbRefTypeEnum.getByCode(entry.getKey());
                Set<Long> dataIdSet = entry.getValue();
                switch (refTypeEnum) {
                    case CONTRACT:
                        contractIds.addAll(dataIdSet);
                        break;
                    case PURCHASE:
                        purchaseIds.addAll(dataIdSet);
                        break;
                    case PRODUCTION_ORDER:
                        productionOrderIds.addAll(dataIdSet);
                        break;
                    case PRODUCT:
                        productIds.addAll(dataIdSet);
                        break;
                    default:
                        break;
                }
            }

            for (Map.Entry<Integer, List<PaasFormDataEntityExt>> entry : saasDataList.entrySet()) {
                Integer businessType = entry.getKey();
                XbbRefTypeEnum refTypeEnum = XbbRefTypeEnum.getByCode(businessType);
                List<PaasFormDataEntityExt> formDataList = entry.getValue();

                switch (refTypeEnum) {
                    case CONTRACT:
                        getLinkProduct(formDataList, contractIds, businessType, ContractEnum.PRODUCT.getAttr(), productIdSet, productMap);
                        break;
                    case PURCHASE:
                        getLinkProduct(formDataList, purchaseIds, businessType, PurchaseEnum.PRODUCT.getAttr(), productIdSet, productMap);
                        break;
                    case PRODUCTION_ORDER:
                        getLinkProduct(formDataList, productionOrderIds, businessType, ProductionOrderEnum.PRODUCT.getAttr(), productIdSet, productMap);
                        break;
                    case PRODUCT:
                        getLinkProduct(formDataList, productIds, businessType, "", productIdSet, productMap);
                        break;
                    default:
                        break;
                }
            }

            if (!productIdSet.isEmpty()) {
                // 产品对应的产品仓库
                BoolQueryBuilder boolQueryBuilder = boolQuery();
                boolQueryBuilder.filter(termQuery("corpid.keyword", corpid));
                boolQueryBuilder.filter(termQuery(StringConstant.DEL, DelEnum.NORMAL.getDel()));
                boolQueryBuilder.filter(termsQuery(ProductWarehouseSubFormEnum.getEsAttr(ProductWarehouseSubFormEnum.PRODUCT_ID), productIdSet));
                boolQueryBuilder.filter(termQuery(ProductWarehouseSubFormEnum.getEsAttr(ProductWarehouseSubFormEnum.WAREHOUSE_CHECKED), 1));
                List<PaasFormDataEntity> productWarehouseList;
                List<String> fieldList = new ArrayList<>();
                fieldList.add(StringConstant.DATA_ID);
                try {
                    productWarehouseList = esHelper.findByScroll(IndexTypeEnum.IDX_SAAS_PRODUCT_WAREHOUSE, boolQueryBuilder, PaasFormDataEntity.class, fieldList);
                } catch (XbbException e) {
                    productWarehouseList = null;
                }

                // 产品对应的产品库存ids, 产品库存ids，库存对应的负责人ids
                Map<Long, Set<Long>> productIdWarehouseIdsMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                Set<Long> warehouseIdSet = new HashSet<>();


                if (Objects.nonNull(productWarehouseList) && !productWarehouseList.isEmpty()) {
                    List<ProductWarehouseEntity> productWarehouseEntities = transferSubFormHelper.transferFormDataToProductWarehouseList(productWarehouseList);
                    for (ProductWarehouseEntity warehouse : productWarehouseEntities) {
                        Long productId = warehouse.getProductId();
                        warehouseIdSet.add(warehouse.getId());
                        if (productIdWarehouseIdsMap.containsKey(productId)) {
                            Set<Long> warehouseIds = productIdWarehouseIdsMap.get(productId);
                            warehouseIds.add(warehouse.getId());
                            productIdWarehouseIdsMap.put(productId, warehouseIds);
                        } else {
                            Set<Long> warehouseIds = new HashSet<>();
                            warehouseIds.add(warehouse.getId());
                            productIdWarehouseIdsMap.put(productId, warehouseIds);
                        }
                    }
                }

                Map<Long, Set<String>> warehouseUserMap = saasAiAssistantStrategyHandle.getWarehouseUser(warehouseIdSet, corpid);

                // 库存负责人--> 产品对应的库存负责人 --> 合同/采购合同/生产单对应的库存负责人
                if (!warehouseUserMap.isEmpty()) {
                    // 产品对应的库存负责人
                    Map<Long, Set<String>> productUserIdsMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                    for (Map.Entry<Long, Set<Long>> productWarehouseEntry : productIdWarehouseIdsMap.entrySet()) {
                        Long productId = productWarehouseEntry.getKey();
                        Set<Long> warehouseIds = productWarehouseEntry.getValue();
                        for (Map.Entry<Long, Set<String>> entry : warehouseUserMap.entrySet()) {
                            Long warehouseId = entry.getKey();
                            Set<String> userIds = entry.getValue();
                            if (!warehouseIds.contains(warehouseId)) {
                                continue;
                            }

                            if (productUserIdsMap.containsKey(productId)) {
                                productUserIdsMap.get(productId).addAll(userIds);
                            } else {
                                productUserIdsMap.put(productId, userIds);
                            }
                        }
                    }

                    // 合同/采购合同/生产单对应的库存负责人
                    for (Map.Entry<Integer, Map<Long, Set<Long>>> entry : productMap.entrySet()) {
                        Integer businessType = entry.getKey();
                        Map<Long, Set<Long>> dataIdProductIdsMap = entry.getValue();
                        linkWarehouseUserIds(productUserIdsMap, dataIdProductIdsMap, businessType, linkWarehouseUserIdsMap);
                    }
                }
            }

        }

        return linkWarehouseUserIdsMap;
    }

    /**
     * saas业务执行人
     *
     * @param corpid                     公司id
     * @param saasSmartList              智能助手列表
     * @param saasDataList               业务数据列表
     * @param businessTypeMap            saas业务类型map{smartId:businessTyp}
     * @param managerLevelMap            负责人map{smartId:[level1,level2]}
     * @param coordinatorLevelMap        协同人map{smartId:[level1,level2]}
     * @param creatorLevelMap            创建人map{smartId:[level1,level2]}
     * @param linkContractLevelMap       关联合同负责人map{smartId:[level1,level2]}
     * @param linkWareHouseLevelMap      关联仓库负责人map{smartId:[level1,level2]}
     * @param dataIdsMap                 数据map{smartId:[data1,data2]}
     * @param userIdsForDept             部门员工map{deptId:[userId1,userId2]}
     * @param userIdsForRole             角色员工map{roleId:[userId1,userId2]}
     * @param userIdsMap                 执行员工map{smartId:[userId1,userId2]}
     * @param deptIdsMap                 执行部门map{smartId:[deptId1,deptId2]}
     * @param roleIdsMap                 执行角色map{smartId:[roleId1,roleId2]}
     * @return 返回执行人
     * @throws
     * @author hongxiao
     * @date 2019-08-09 11:29
     * @version v1.0
     * @since v1.0
     */
    private List<SsExecutorPojo> getSaasExecutorList(String corpid, List<AiAssistantEntity> saasSmartList, Map<Integer, List<PaasFormDataEntityExt>> saasDataList,
                                                     Map<Long, Integer> businessTypeMap,
                                                     Map<Long, Set<Integer>> managerLevelMap, Map<Long, Set<Integer>> coordinatorLevelMap, Map<Long, Set<Integer>> creatorLevelMap,
                                                     Map<Long, Set<Integer>> linkContractLevelMap, Map<Long, Set<Integer>> linkWareHouseLevelMap, Map<Long, Set<Integer>> linkPurchaseLevelMap,
                                                     Map<Long, Set<Long>> dataIdsMap, Map<Long, List<String>> userIdsForDept, Map<Integer, List<String>> userIdsForRole,
                                                     Map<Long, Set<String>> userIdsMap, Map<Long, Set<Long>> deptIdsMap, Map<Long, Set<Integer>> roleIdsMap) {
        // 关联负责人对应的数据id
        Map<Integer, Set<Long>> saasManagerDataIdsMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        Map<Integer, Set<Long>> saasCoordinatorDataIdsMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
//        Map<Integer, Set<Long>> saasCreatorDataIdsMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        Set<Long> managerDataIds = new HashSet<>();
        Set<Long> coordinatorDataIds = new HashSet<>();
//        Set<Long> creatorDataIds = new HashSet<>();

        // 负责人
        getLevelDataIds(managerLevelMap, dataIdsMap, businessTypeMap, saasManagerDataIdsMap, managerDataIds);
        // 协同人
        getLevelDataIds(coordinatorLevelMap, dataIdsMap, businessTypeMap, saasCoordinatorDataIdsMap, coordinatorDataIds);
//        getLevelDataIds(creatorLevelMap, dataIdsMap, businessTypeMap, saasCreatorDataIdsMap, creatorDataIds);
        // 定义一个新的变量
        // 获取关联的合同、仓库的数据id、关联的采购合同
        Map<Integer, Set<Long>> linkContractDataIdsMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        Map<Integer, Set<Long>> linkWareHouseDataIdsMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        Map<Integer, Set<Long>> linkPurchaseDataIdsMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        getLinkLevelDatdIds(linkContractLevelMap, dataIdsMap, businessTypeMap, linkContractDataIdsMap);
        getLinkLevelDatdIds(linkWareHouseLevelMap, dataIdsMap, businessTypeMap, linkWareHouseDataIdsMap);
        getLinkLevelDatdIds(linkPurchaseLevelMap, dataIdsMap, businessTypeMap, linkPurchaseDataIdsMap);

        Map<Integer, Set<Long>> allDataIdsMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        allDataIdsMap.putAll(saasManagerDataIdsMap);
        saasCoordinatorDataIdsMap.forEach((key, value)->{
            if (allDataIdsMap.containsKey(key)) {
                Set<Long> dataValue = allDataIdsMap.get(key);
                dataValue.addAll(saasCoordinatorDataIdsMap.get(key));
                allDataIdsMap.put(key, dataValue);
            } else {
                allDataIdsMap.put(key, value);
            }
        });

        /* ================== 动态负责人 start =============== */

        // 回款对应的合同负责人
        Map<Long, Set<String>> linkContractUserMap = getLinkContrOrPurUserIds(corpid, saasDataList, linkContractDataIdsMap, XbbRefTypeEnum.PAYMENT);
        // 付款对应的采购合同负责人
        Map<Long, Set<String>> linkPurchaseUserMap = getLinkContrOrPurUserIds(corpid, saasDataList, linkPurchaseDataIdsMap, XbbRefTypeEnum.PAY_PLAN);

        // 仓库负责人
        Map<Integer, Map<Long, Set<String>>> linkWarehouseUserIdsMap = getLinkWarehouseUserId(corpid, linkWareHouseDataIdsMap, saasDataList);

        // 动态人员
        Set<String> dynamicUserIdSet = new HashSet<>();

        // 回款关联的合同负责人
        if (!linkContractUserMap.isEmpty()) {
            linkContractUserMap.forEach((paymentId, userIds) -> {
                if (CollectionsUtil.isNotEmpty(userIds)) {
                    dynamicUserIdSet.addAll(userIds);
                }
            });
        }
        // 付款关联的采购合同负责人
        if (!linkPurchaseUserMap.isEmpty()) {
            linkPurchaseUserMap.forEach((payPlanId, userIds) -> {
                if (CollectionsUtil.isNotEmpty(userIds)) {
                    dynamicUserIdSet.addAll(userIds);
                }
            });
        }

        // 关联仓库负责人
        if(!linkWarehouseUserIdsMap.isEmpty()) {
            linkWarehouseUserIdsMap.forEach((businessType, dataLinkWarehouseUserMap) -> {
                if (dataLinkWarehouseUserMap != null) {
                    dataLinkWarehouseUserMap.forEach((dataId, userIds) -> {
                        if (CollectionsUtil.isNotEmpty(userIds)) {
                            dynamicUserIdSet.addAll(userIds);
                        }
                    });
                }
            });
        }

        // saas入库的负责人/协同人{businessType:{dataId1:[userId1,userId2],{data2:[userId1,userId2]}}}
        Map<Integer, Map<Long, List<String>>> managerUserIdsMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        Map<Integer, Map<Long, List<String>>> coorUserIdsMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        allDataIdsMap.forEach((businessType, dataIds) -> {
            // 产品时dataIds需要父产品的id
            if (Objects.equals(XbbRefTypeEnum.PRODUCT.getCode(), businessType)) {
                Set<Long> parentIdList = new HashSet<>();
                List<PaasFormDataEntityExt> productList = saasDataList.get(businessType);
                for (PaasFormDataEntityExt entityExt : productList) {
                    if (dataIds.contains(entityExt.getId())) {
                        JSONObject data = entityExt.getData();
                        Long parentId = data.getLong(ProductEnum.PARENT_ID.getAttr());
                        if (Objects.equals(parentId, 0L)) {
                            parentIdList.add(entityExt.getId());
                        } else {
                            parentIdList.add(parentId);
                        }
                    }
                }
                dataIds.clear();
                dataIds.addAll(parentIdList);
            }
            // TODO dataIds数据量大也要分页处理
            List<UserTeamEntity> userTeamEntityList = userTeamHelp.getUserTeamList(dataIds, corpid, businessType, false, null);
            Map<Long, List<String>> mainUserMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            Map<Long, List<String>> coUserMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            for (UserTeamEntity entity : userTeamEntityList) {
                if (Objects.equals(BasicConstant.ONE, entity.getIsMain()) && !saasManagerDataIdsMap.isEmpty()) {
                    // 负责人
                    setUserTeam(mainUserMap, entity.getUserId(), entity.getDataId());
                } else if (!saasCoordinatorDataIdsMap.isEmpty()) {
                    // 协同人
                    setUserTeam(coUserMap, entity.getUserId(), entity.getDataId());
                }
                dynamicUserIdSet.add(entity.getUserId());
            }
            // 负责人
            if (!mainUserMap.isEmpty()) {
                managerUserIdsMap.put(businessType, mainUserMap);
            }
            // 协同人
            if (!coUserMap.isEmpty()) {
                coorUserIdsMap.put(businessType, coUserMap);
            }

        });


        // 部门对应的负责人
        Map<Long, List<String>> userInDeptMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        // 部门对应的四级部门
        Map<Long, List<Long>> fourDeptMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        // 部门对应的主管
        Map<Long, List<String>> userDeptMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);

        getDynamicUserDept(corpid, dynamicUserIdSet, userInDeptMap, fourDeptMap, userDeptMap);

        /* ================== 动态负责人 end =============== */


        List<SsExecutorPojo> resultList = new ArrayList<>();

        // 模块数据对应执行人
        for (AiAssistantEntity entity : saasSmartList) {
            Long smartId = entity.getId();
            Integer businessType = entity.getBusinessType();
            // 当前需要执行的人员
            Set<String> execUserIds = normalExecutorList(userIdsForDept, userIdsForRole, userIdsMap, deptIdsMap, roleIdsMap, smartId);

            // 动态执行人级别列表
            Set<Integer> managerLeveList = managerLevelMap.get(smartId);
            Set<Integer> coLevelList = coordinatorLevelMap.get(smartId);
            Set<Integer> creatorLevelList = creatorLevelMap.get(smartId);
            Set<Integer> linkContractLevelList = linkContractLevelMap.get(smartId);
            Set<Integer> linkWareHouseLevelList = linkWareHouseLevelMap.get(smartId);
            Set<Integer> linkPurchaseLevelList = linkPurchaseLevelMap.get(smartId);

            // 动态执行人--负责人、协同人
            Map<Long, List<String>> managerUserMap = managerUserIdsMap.get(businessType);
            Map<Long, List<String>> coUserMap = coorUserIdsMap.get(businessType);

            // 关联仓库负责人
            Map<Long, Set<String>> dataLinkWarehouseMap = linkWarehouseUserIdsMap.get(businessType);


            // 动态负责人
            Set<Long> dataIds = dataIdsMap.get(entity.getId());
            if (Objects.nonNull(dataIds)) {
                for (Long dataId : dataIds) {
                    // 负责人
                    List<String> managerUserIds = managerUserMap != null && managerUserMap.containsKey(dataId) ? managerUserMap.get(dataId) : new ArrayList<>();
                    // 协同人
                    List<String> coUserIds = coUserMap != null && coUserMap.containsKey(dataId) ? coUserMap.get(dataId) : new ArrayList<>();
                    // 关联合同负责人
                    List<String> linkContractUserIds = new ArrayList<>();
                    if (Objects.equals(XbbRefTypeEnum.PAYMENT.getCode(), businessType) && Objects.nonNull(linkContractUserMap) && linkContractUserMap.containsKey(dataId)) {
                        linkContractUserIds = new ArrayList<>(linkContractUserMap.get(dataId));
                    }
                    // 关联采购合同负责人
                    List<String> linkPurchasetUserIds = new ArrayList<>();
                    if (Objects.equals(XbbRefTypeEnum.PAY_PLAN.getCode(), businessType) && Objects.nonNull(linkPurchaseUserMap) && linkPurchaseUserMap.containsKey(dataId)) {
                        linkPurchasetUserIds = new ArrayList<>(linkPurchaseUserMap.get(dataId));
                    }
                    // 关联仓库负责人
                    List<String> linkWarehouseUserIds = new ArrayList<>();
                    if(Objects.nonNull(dataLinkWarehouseMap) && dataLinkWarehouseMap.containsKey(dataId)) {
                        linkWarehouseUserIds = new ArrayList<>(dataLinkWarehouseMap.get(dataId));
                    }

                    Set<String> dynamicExecutorList = dynamicExecutorList(managerLeveList, coLevelList, creatorLevelList,
                            linkContractLevelList, linkWareHouseLevelList, linkPurchaseLevelList, managerUserIds, coUserIds,
                            linkContractUserIds, linkPurchasetUserIds, linkWarehouseUserIds, userInDeptMap, fourDeptMap, userDeptMap);

                    if (Objects.equals("ding51d0a413630fcca735c2f4657eb6378f", corpid)) {
                        LOG.error("SsExecutorServiceImpl:getSaasExecutorList:" + JSON.toJSONString(dynamicExecutorList));
                    }
                    // 所有的执行人
                    execUserIds.addAll(dynamicExecutorList);
                    // 执行人实体
                    List<UserEntity> execUserList = userModel.getByUserIdIn(corpid, execUserIds);
                    // 拼接执行人数据
                    SsExecutorPojo pojo = new SsExecutorPojo();
                    pojo.setSmartSettingsId(entity.getId());
                    pojo.setFormId(entity.getFormId());
                    pojo.setDataId(dataId);
                    pojo.setExecutorIdList(new ArrayList<>(execUserIds));
                    pojo.setExecutorList(execUserList);

                    resultList.add(pojo);
                }
            }
        }

        return resultList;
    }


    /**
     * paas业务执行人
     *
     * @param corpid              公司id
     * @param paasSmartList       智能助手列表
     * @param paasDataList        业务数据列表
     * @param managerLevelMap     负责人map{smartId:[level1,level2]}
     * @param coordinatorLevelMap 协同人map{smartId:[level1,level2]}
     * @param creatorLevelMap     创建人map{smartId:[level1,level2]}
     * @param dataIdsMap          数据map{smartId:[data1,data2]}
     * @param userIdsForDept      部门员工map{deptId:[userId1,userId2]}
     * @param userIdsForRole      角色员工map{roleId:[userId1,userId2]}
     * @param userIdsMap          执行员工map{smartId:[userId1,userId2]}
     * @param deptIdsMap          执行部门map{smartId:[deptId1,deptId2]}
     * @param roleIdsMap          执行角色map{smartId:[roleId1,roleId2]}
     * @return
     * @throws
     * @author hongxiao
     * @date 2019-08-16 19:12
     * @version v1.0
     * @since v1.0
     */
    private List<SsExecutorPojo> getPaasExecutorList(String corpid, List<AiAssistantEntity> paasSmartList, List<PaasFormDataEntityExt> paasDataList,
                                                     Map<Long, Set<Integer>> managerLevelMap, Map<Long, Set<Integer>> coordinatorLevelMap, Map<Long, Set<Integer>> creatorLevelMap,
                                                     Map<Long, Set<Long>> dataIdsMap, Map<Long, List<String>> userIdsForDept, Map<Integer, List<String>> userIdsForRole,
                                                     Map<Long, Set<String>> userIdsMap, Map<Long, Set<Long>> deptIdsMap, Map<Long, Set<Integer>> roleIdsMap) {
        /* ================== 动态负责人 start =============== */
        // 动态人员
        Set<String> dynamicUserIdSet = new HashSet<>();
        // {data1:[userId1,userId2]}
        Map<Long, List<String>> mainUserMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
//        Map<Long, List<String>> coUserMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        for (PaasFormDataEntityExt entityExt : paasDataList) {
            // 负责人
            setUserTeam(mainUserMap, entityExt.getOwnerId(), entityExt.getId());
            dynamicUserIdSet.add(entityExt.getOwnerId());
            // TODO 协同人还没实现，tb_paas_co_user
        }

        // 部门对应的负责人
        Map<Long, List<String>> userInDeptMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        // 部门对应的四级部门
        Map<Long, List<Long>> fourDeptMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        // 部门对应的主管
        Map<Long, List<String>> userDeptMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);

        getDynamicUserDept(corpid, dynamicUserIdSet, userInDeptMap, fourDeptMap, userDeptMap);

        /* ================== 动态负责人 end =============== */

        List<SsExecutorPojo> resultList = new ArrayList<>();
        for (AiAssistantEntity entity : paasSmartList) {
            // 当前需要执行的人员
            Set<String> execUserIds = normalExecutorList(userIdsForDept, userIdsForRole, userIdsMap, deptIdsMap, roleIdsMap, entity.getId());
            // 动态执行人级别列表
            Set<Integer> managerLeveList = managerLevelMap.get(entity.getId());
            Set<Integer> coorLevelList = coordinatorLevelMap.get(entity.getId());
            Set<Integer> creatorLevelList = creatorLevelMap.get(entity.getId());


            Set<Long> dataIds = dataIdsMap.get(entity.getId());
            if (Objects.nonNull(dataIds)) {
                for (Long dataId : dataIds) {
                    // 负责人
                    List<String> managerUserIds = mainUserMap != null && mainUserMap.containsKey(dataId) ? mainUserMap.get(dataId) : new ArrayList<>();
                    // TODO 协同人

                    Set<String> dynamicExecutorList = dynamicExecutorList(managerLeveList, coorLevelList, creatorLevelList,
                            null, null, null, managerUserIds, null,
                            null, null, null, userInDeptMap, fourDeptMap, userDeptMap);

                    // 所有的执行人
                    execUserIds.addAll(dynamicExecutorList);
                    // 执行人实体
                    List<UserEntity> execUserList = userModel.getByUserIdIn(corpid, execUserIds);
                    // 拼接执行人数据
                    SsExecutorPojo pojo = new SsExecutorPojo();
                    pojo.setSmartSettingsId(entity.getId());
                    pojo.setFormId(entity.getFormId());
                    pojo.setDataId(dataId);
                    pojo.setExecutorIdList(new ArrayList<>(execUserIds));
                    pojo.setExecutorList(execUserList);

                    resultList.add(pojo);
                }
            }
        }

        return resultList;
    }

    /**
     * 工作报告执行人
     *
     * @param corpid         公司id
     * @param smartList      智能助手
     * @param userIdsForDept 部门对应的员工列表
     * @param userIdsForRole 角色对应的员工列表
     * @param userIdsMap     智能助手设置的执行成员
     * @param deptIdsMap     智能助手设置的执行部门
     * @param roleIdsMap     智能助手设置的执行角色
     * @param workReportExecutorListMap 工作报告执行人列表
     * @return
     * @throws
     * @author hongxiao
     * @date 2019-08-08 20:58
     * @version v1.0
     * @since v1.0
     */
    private void getWorkReportExecutorList(String corpid, List<AiAssistantEntity> smartList, Map<Long, List<String>> userIdsForDept,
                                                           Map<Integer, List<String>> userIdsForRole, Map<Long, Set<String>> userIdsMap,
                                                           Map<Long, Set<Long>> deptIdsMap, Map<Long, Set<Integer>> roleIdsMap,
                                           Map<Long, SsExecutorPojo> workReportExecutorListMap) {
        if (Objects.isNull(smartList) || smartList.isEmpty()) {
            return ;
        }

        // 当前日期，默认周报
        Integer now = DateUtil.getTodayInt();
        Integer reportType = WorkReportTypeEnum.WEEKLY.getType();

        List<UserEntity> adminList = userAndDepartmentHelp.getAdminOrBoss(corpid);
        String adminUserId = null;
        if(!adminList.isEmpty()) {
            adminUserId = adminList.get(0).getUserId();
        }
        if(adminUserId == null) {
            return ;
        }

        // 获取未写工作报告的人
        WorkReportUserStatisicDTO workReportUserStatisicDTO = new WorkReportUserStatisicDTO();
        workReportUserStatisicDTO.setCorpid(corpid);
        // 超管id
        workReportUserStatisicDTO.setUserId(adminUserId);
        workReportUserStatisicDTO.setPlatform(StringConstant.PLATFORM_WEB);
        workReportUserStatisicDTO.setPageDone(BasicConstant.ONE);
        workReportUserStatisicDTO.setPageUnDone(BasicConstant.ONE);

        for (AiAssistantEntity entity : smartList) {
            LOG.error("SsExecutorServiceImpl:getWorkReportExecutorList:" + JSON.toJSONString(entity.getExecutor()));
            // 当前需要执行的人员
            Set<String> execUserIds = normalExecutorList(userIdsForDept, userIdsForRole, userIdsMap, deptIdsMap, roleIdsMap, entity.getId());

            List<ConditionsEntityExt> conditionsEntityExtList = entity.getTriggerCondition().toJavaList(ConditionsEntityExt.class);
            if (!conditionsEntityExtList.isEmpty()) {
                // 获取类型，用于区分日报、月报、周报
                ConditionsEntityExt conditionsEntityExt = conditionsEntityExtList.get(0);
                reportType = conditionsEntityExt.getFieldType();
            }
            if (Objects.equals(WorkReportTypeEnum.WEEKLY.getType(), reportType)) {
                now = DateUtil.getThisWeekMorning();
            } else if (Objects.equals(WorkReportTypeEnum.MONTHLY.getType(), reportType)) {
                now = DateUtil.getFirstDateOfThisMonth();
            }
            // 工作报告日期
            String startDay = DateUtil.getDateString(now);
            String endDay = DateUtil.getDateString(now + TimeConstant.SECONDS_PER_DAY);
            JSONArray reportDateArray = new JSONArray();
            reportDateArray.add(startDay);
            reportDateArray.add(endDay);

            workReportUserStatisicDTO.setReportType(reportType);
            workReportUserStatisicDTO.setReportDate(reportDateArray);

            WorkReportUserStatisicVO userStatisicVO;
            try {
                Locale locale = LocaleContextHolder.getLocale();
                String lang = locale.toString();
                String userStatisticData = businessFeignClient.workReportUserStatisticsList(workReportUserStatisicDTO, lang);
                JSONObject resultObj = JsonHelperUtil.parseObject(userStatisticData);
                if (ErrorCodeEnum.API_SUCCESS.getCode() != (int)resultObj.get(StringConstant.CODE)) {
                    throw new XbbException(resultObj.getIntValue(StringConstant.CODE),resultObj.getString(StringConstant.KEY_ERROR_MSG));
                }
                userStatisicVO = JsonHelperUtil.parseObject(resultObj.getString("result"),WorkReportUserStatisicVO.class);
            } catch (XbbException e) {
                continue;
            }

            List<WorkReportUserStatisicPojo> unDoneUsers = userStatisicVO.getUnDoneUsers();
            if (Objects.isNull(unDoneUsers) || unDoneUsers.isEmpty()) {
                continue;
            }

            // 与需要执行的人员取交集
            if (!execUserIds.isEmpty()) {
                Iterator<WorkReportUserStatisicPojo> iterator = unDoneUsers.iterator();
                while (iterator.hasNext()) {
                    WorkReportUserStatisicPojo pojo = iterator.next();
                    if (!execUserIds.contains(pojo.getUserId())) {
                        iterator.remove();
                    }
                }
            }

            LOG.error("SsExecutorServiceImpl:getWorkReportExecutorList:unDoneUsers:" + JSON.toJSONString(unDoneUsers));
            if (!unDoneUsers.isEmpty()) {
                List<String> userIds = new ArrayList<>();
                List<UserEntity> userList = new ArrayList<>();
                for (WorkReportUserStatisicPojo pojo : unDoneUsers) {
                    // 员工ids
                    userIds.add(pojo.getUserId());

                    // 员工实体
                    UserEntity userEntity = new UserEntity();
                    userEntity.setCorpid(corpid);
                    userEntity.setUserId(pojo.getUserId());
                    userEntity.setName(pojo.getUserName());
                    userEntity.setAvatar(pojo.getUserAvatar());
                    userList.add(userEntity);
                }


                // 拼接执行人数据
                SsExecutorPojo pojo = new SsExecutorPojo();
                pojo.setSmartSettingsId(entity.getId());
                pojo.setFormId(entity.getFormId());
                pojo.setExecutorIdList(userIds);
                pojo.setExecutorList(userList);

                workReportExecutorListMap.put(entity.getId(), pojo);
            }
        }

    }

    /**
     * 获取常用执行人员
     *
     * @param userIdsForDept 部门员工map{deptId:[userId1,userId2]}
     * @param userIdsForRole 角色员工map{roleId:[userId1,userId2]}
     * @param userIdsMap     执行员工map{smartId:[userId1,userId2]}
     * @param deptIdsMap     执行部门map{smartId:[deptId1,deptId2]}
     * @param roleIdsMap     执行角色map{smartId:[roleId1,roleId2]}
     * @return
     * @throws
     * @author hongxiao
     * @date 2019-08-09 15:52
     * @version v1.0
     * @since v1.0
     */
    private Set<String> normalExecutorList(Map<Long, List<String>> userIdsForDept, Map<Integer, List<String>> userIdsForRole,
                                           Map<Long, Set<String>> userIdsMap, Map<Long, Set<Long>> deptIdsMap, Map<Long, Set<Integer>> roleIdsMap, Long smartId) {
        // 当前需要执行的人员
        Set<String> execUserIds = userIdsMap.get(smartId);
        Set<Long> execDepIds = deptIdsMap.get(smartId);
        Set<Integer> execRoleIds = roleIdsMap.get(smartId);

        if (Objects.isNull(execUserIds)) {
            execUserIds = new HashSet<>();
        }

        // 添加部门执行人
        if (Objects.nonNull(execDepIds) && !execDepIds.isEmpty()) {
            for (Long depId : execDepIds) {
                if (userIdsForDept.containsKey(depId)) {
                    execUserIds.addAll(userIdsForDept.get(depId));
                }
            }
        }
        // 添加角色执行人
        if (Objects.nonNull(execRoleIds) && !execRoleIds.isEmpty()) {
            for (Integer roleId : execRoleIds) {
                if (userIdsForRole.containsKey(roleId)) {
                    execUserIds.addAll(userIdsForRole.get(roleId));
                }
            }
        }

        return execUserIds;
    }

    /**
     * 获取动态执行人对应的部门/主管数据
     *
     * @param corpid           公司id
     * @param dynamicUserIdSet 动态执行人ids
     * @param userInDeptMap    部门对应的员工{depId:[userId1,userId2]}
     * @param fourDeptMap      部门对应的四级部门ids{depId:[depId4,depId3,depId2,depId1]}
     * @param userDeptMap      部门对应的主管{depId:[userId1,userId2]}
     * @return
     * @throws
     * @author hongxiao
     * @date 2019-08-16 16:31
     * @version v1.0
     * @since v1.0
     */
    private void getDynamicUserDept(String corpid, Set<String> dynamicUserIdSet, Map<Long, List<String>> userInDeptMap,
                                    Map<Long, List<Long>> fourDeptMap, Map<Long, List<String>> userDeptMap) {
        // 获取所有动态负责人（负责人/协同人等）实体
        List<UserEntity> userList = userModel.getByUserIdIn(corpid, dynamicUserIdSet);

        // 负责人所属部门的ids
        Set<Long> deptIdSet = new HashSet<>();
        userList.forEach(item -> {
            List<Long> deptIds = JsonHelperUtil.parseArray(item.getDepartment(), Long.class);
            Long deptId = 1L;
            if (deptIds.size() > 0) {
                deptId = deptIds.get(0);
            }
            deptIdSet.add(deptId);
            if (userInDeptMap.containsKey(deptId)) {
                userInDeptMap.get(deptId).add(item.getUserId());
            } else {
                List<String> userIdList = new ArrayList<>();
                userIdList.add(item.getUserId());
                userInDeptMap.put(deptId, userIdList);
            }
        });

        // 获取负责人对应的部门列表
        List<DepartmentEntity> allDepList = userAndDepartmentHelp.getDepListByIds(corpid, deptIdSet);
        // 需要查找主管的部门（最多四级主管）
        // 部门对应的四级部门
        Set<Long> managerDeptIdSet = new HashSet<>();
        allDepList.forEach(item -> {
            List<Long> deptIds = StringUtil.toLongList(item.getDepIdRouter(), "|");
            int size = deptIds.size();
            List<Long> fourDeptIds = new ArrayList<>(deptIds);
            if (size > MAX_LEVEL_LEADER) {
                // 取最后四级的部门
                fourDeptIds = deptIds.subList(size - MAX_LEVEL_LEADER, size);
            }
            managerDeptIdSet.addAll(fourDeptIds);
            fourDeptMap.put(item.getId(), fourDeptIds);
        });
        // 添加顶级部门
        managerDeptIdSet.add(1L);

        // 4.3.部门对应的主管
        List<UserDepartmentEntity> userDepartmentList = userAndDepartmentHelp.getUserDepListByDepIds(corpid, managerDeptIdSet, BasicConstant.ONE);
        if (Objects.equals("ding51d0a413630fcca735c2f4657eb6378f", corpid)) {
            LOG.error("SsExecutorServiceImpl:getDynamicUserDept:departManager:" + JSON.toJSONString(managerDeptIdSet) + "     userDepartmentList:" + JSON.toJSONString(userDepartmentList));
        }

        userDepartmentList.forEach(item -> {
            Long deptId = item.getDepartmentId();
            if (userDeptMap.containsKey(deptId)) {
                userDeptMap.get(deptId).add(item.getUserId());
            } else {
                List<String> userIdList = new ArrayList<>();
                userIdList.add(item.getUserId());
                userDeptMap.put(deptId, userIdList);
            }
        });
    }

    /**
     * 获取动态执行人
     *
     * @param managerLeveList        负责人级别列表
     * @param coLevelList          协同人级别列表
     * @param creatorLevelList       创建人级别列表
     * @param linkContractLevelList  关联合同负责人级别列表
     * @param linkWareHouseLevelList 关联仓库负责人级别列表
     * @param managerUserIds         负责人列表
     * @param coUserIds              协同人列表
     * @param linkContractUserIds    关联合同负责人列表
     * @param linkWarehouseUserIds   关联仓库负责人列表
     * @param userInDeptMap          员工所在部门{depId:[userId1,userId2]}
     * @param fourDeptMap            部门对应的四级上级部门{depId:[depRoutIds]}
     * @param userDeptMap            部门对应的主管{depId:[userId1,userId2]}
     * @return 所有的动态执行人列表
     * @throws
     * @author hongxiao
     * @date 2019-08-12 15:58
     * @version v1.0
     * @since v1.0
     */
    private Set<String> dynamicExecutorList(Set<Integer> managerLeveList, Set<Integer> coLevelList,
                                            Set<Integer> creatorLevelList, Set<Integer> linkContractLevelList, Set<Integer> linkWareHouseLevelList, Set<Integer> linkPurchaseLevelList,
                                            List<String> managerUserIds, List<String> coUserIds, List<String> linkContractUserIds, List<String> linkPurchaseUserIds,List<String> linkWarehouseUserIds,
                                            Map<Long, List<String>> userInDeptMap, Map<Long, List<Long>> fourDeptMap, Map<Long, List<String>> userDeptMap) {

        Set<String> resultSet = new HashSet<>();

        // 负责人
        Set<String> managerUserIdSet = getDynamicUserIds(managerLeveList, managerUserIds, userInDeptMap, fourDeptMap, userDeptMap);
        // 协同人
        Set<String> coorUserIdSet = getDynamicUserIds(coLevelList, coUserIds, userInDeptMap, fourDeptMap, userDeptMap);
        // 创建人

        // 关联合同负责人
        Set<String> linkContractUserIdSet = getDynamicUserIds(linkContractLevelList, linkContractUserIds, userInDeptMap, fourDeptMap, userDeptMap);
        Set<String> linkPurchaseUserIdSet = getDynamicUserIds(linkPurchaseLevelList, linkPurchaseUserIds, userInDeptMap, fourDeptMap, userDeptMap);
        // 关联仓库负责人
        Set<String> linkWarehouseUserIdSet = getDynamicUserIds(linkWareHouseLevelList, linkWarehouseUserIds, userInDeptMap, fourDeptMap, userDeptMap);

        resultSet.addAll(managerUserIdSet);
        resultSet.addAll(coorUserIdSet);
        resultSet.addAll(linkContractUserIdSet);
        resultSet.addAll(linkPurchaseUserIdSet);
        resultSet.addAll(linkWarehouseUserIdSet);

        return resultSet;
    }

    /**
     * 获取各级主管对应的部门（一级/二级/三级/四级/顶级）
     *
     * @param userInDeptMap  员工所在部门{depId:[userId1,userId2]}
     * @param fourDeptMap    部门对应的四级上级部门{depId:[depRoutIds]}
     * @param firstDepList   一级部门
     * @param secondDepList  二级部门
     * @param thirdDepList   三级部门
     * @param fourthDepList  四级部门
     * @return
     * @throws
     * @author hongxiao
     * @date 2019-08-12 16:34
     * @version v1.0
     * @since v1.0
     */
    private void getDynamicDepForLevel(Map<Long, List<String>> userInDeptMap, Map<Long, List<Long>> fourDeptMap,
                                       List<Long> firstDepList, List<Long> secondDepList, List<Long> thirdDepList, List<Long> fourthDepList) {
        for (Map.Entry<Long, List<String>> entry : userInDeptMap.entrySet()) {
            Long depId = entry.getKey();
            firstDepList.add(depId);
        }
        for (Long depId : firstDepList) {
            List<Long> depRoutIds = fourDeptMap.get(depId);
            if (Objects.isNull(depRoutIds) || depRoutIds.isEmpty()) {
                continue;
            }

            int size = depRoutIds.size();
            // 二级主管部门
            secondDepList.add(depRoutIds.get(size - 1));
            // 三级主管部门
            if (size - 2 >= 0) {
                thirdDepList.add(depRoutIds.get(size - 2));
            }
            // 四级主管部门
            if (size - 3 >= 0) {
                fourthDepList.add(depRoutIds.get(size - 3));
            }
        }
    }

    /**
     * 获取动态执行人
     *
     * @param leveList       负责人级别列表
     * @param dynamicUserIds 动态负责人/协同人/创建人/关联合同负责人/关联仓库负责人/关联采购合同负责人
     * @param userInDeptMap  员工所在部门{depId:[userId1,userId2]}
     * @param userDeptMap    部门对应的主管{depId:[userId1,userId2]}
     * @param fourDeptMap    部门对应的四级上级部门{depId:[depRoutIds]}
     * @return
     * @throws
     * @author hongxiao
     * @date 2019-08-12 16:42
     * @version v1.0
     * @since v1.0
     */
    private Set<String> getDynamicUserIds(Set<Integer> leveList, List<String> dynamicUserIds, Map<Long, List<String>> userInDeptMap,
                                          Map<Long, List<Long>> fourDeptMap, Map<Long, List<String>> userDeptMap) {

        Set<String> resultSet = new HashSet<>();
        if (Objects.nonNull(leveList) && !leveList.isEmpty()) {
            List<Long> firstDepList = new ArrayList<>();
            List<Long> secondDepList = new ArrayList<>();
            List<Long> thirdDepList = new ArrayList<>();
            List<Long> fourthDepList = new ArrayList<>();
            // 对应级别的部门
            getDynamicDepForLevel(userInDeptMap, fourDeptMap, firstDepList, secondDepList, thirdDepList, fourthDepList);


            for (Integer code : leveList) {
                DynamicManagerLevelEnum levelEnum = DynamicManagerLevelEnum.getByLevel(code);
                switch (levelEnum) {
                    case ZERO_LEVEL:
                        resultSet.addAll(dynamicUserIds);
                        break;
                    case ONE_LEVEL:
                        for (Long depId : firstDepList) {
                            List<String> userIds = userDeptMap.get(depId);
                            if (Objects.nonNull(userIds)) {
                                resultSet.addAll(userIds);
                            }
                        }
                        break;
                    case TWO_LEVEL:
                        for (Long depId : secondDepList) {
                            List<String> userIds = userDeptMap.get(depId);
                            if (Objects.nonNull(userIds)) {
                                resultSet.addAll(userIds);
                            }
                        }
                        break;
                    case THREE_LEVEL:
                        for (Long depId : thirdDepList) {
                            List<String> userIds = userDeptMap.get(depId);
                            if (Objects.nonNull(userIds)) {
                                resultSet.addAll(userIds);
                            }
                        }
                        break;
                    case FOUR_LEVEL:
                        for (Long depId : fourthDepList) {
                            List<String> userIds = userDeptMap.get(depId);
                            if (Objects.nonNull(userIds)) {
                                resultSet.addAll(userIds);
                            }
                        }
                        break;
                    case TOP_LEVEL:
                        List<String> userIds = userDeptMap.get(1L);
                        if (Objects.nonNull(userIds)) {
                            resultSet.addAll(userIds);
                        }
                        break;
                    default:
                        break;
                }
            }

        }

        return resultSet;
    }
}
